/*
 *  xml_connection.h
 *  Untitled
 *
 *  Created by Ben Nolan on 3/11/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#pragma once

#include <deque>
#include <iostream>
#include <boost/asio.hpp>

namespace Wonderland{

class Message;

class XmlConnection{
public:
	XmlConnection(boost::asio::io_service& io_service) : socket_(io_service), writing_(false){
	}

	boost::asio::ip::tcp::socket& socket(){
		return socket_;
	}

	virtual void onDisconnected() {};
	virtual void onConnected() {};
	virtual void onMessage(Message&) {};
	
	void acceptConnection();
	void sendMessage(const Message*);

protected:
	void checkMessageQueue();
	void onProtocolFailure();
//	void onMessageHeader(const boost::system::error_code& error);
	void onMessageBody(const boost::system::error_code& error);
	void onWriteComplete(const boost::system::error_code& error);
	void prepareForMessage();

//	char header_[4];
	bool writing_;
	std::deque<const Message*> messages_;
//	std::vector<char> body_;
	boost::asio::streambuf body_;
	boost::asio::ip::tcp::socket socket_;

};

}


